<%--
  Created by IntelliJ IDEA.
  User: ChadZhao
  Date: 12-2-9
  Time: 下午8:13
--%>

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>用户管理</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
    <link rel="stylesheet" type="text/css" href="<c:url value='/styles/base.css'/>"/>
    <link rel="stylesheet" type="text/css" href="<c:url value='/styles/main.css'/>"/>
    <style type="text/css">
        <!--
        .tr0 {
        }

        .tr1 {
            background-color: #f4f4f4;
        }
        body,td,th {
            font-size: 14px;
            color: #666666;
        }
        .form1 { width:99%; padding:5px;}
        .form1 th { text-align:right; font-weight:bold; padding:5px;}
        .form1 td { text-align:left; padding:5px;}
        .listinput { border:1px solid #ccc; padding:3px; background-image:url(<c:url value='/images/listinputbg.gif'/>); width:200px;}
        select  { border:1px solid #ccc; padding:3px;}
        button { background-image:url(<c:url value='/images/butbg.gif'/>); height:30px; line-height:30px; color:#FFFFFF; font-weight:bold; border:0px; cursor:hand;}
        -->
    </style>
</head>
<body leftmargin="8" topmargin='8'>
<form method="post">
<table width="98%" border="0" align="center" cellpadding="0" cellspacing="0">
    <tr>
        <td>
            <div style='float:left'><img height="14" src="<c:url value='/frame/book1.gif'/>" width="20"/>&nbsp;用户管理
            </div>
            <div style='float:right;padding-right:8px;'>
                <!--  //保留接口  -->
            </div>
        </td>
    </tr>
    <tr>
        <td height="1" background="<c:url value='/frame/sp_bg.gif'/>" style='padding:0px'></td>
    </tr>
</table>
    <table width="98%" align="center" border="0" cellpadding="5" cellspacing="1" bgcolor="#CBD8AC"
       style="margin-bottom:8px;margin-top:8px;">
    <tr>
        <td background="<c:url value='/frame/wbg.gif'/>" bgcolor="#EEF4EA" class='title'>
            <span><img align="absmiddle" src="<c:url value='/frame/ico.jpg'/>" width="13" height="15"> 所有用户</span></td>
    </tr>
    <tr bgcolor="#FFFFFF">
        <td align="center">
            <table width="99%" border="0" cellpadding="0" cellspacing="0" id="queryResult">
                <tr align="center" style="padding:0px;background-image:url(<c:url value='/frame/lititbg_06.jpg'/>)">
                    <td style="height:29px;padding:0px;"><strong>选择</strong></td>
                    <td style="padding:0px;"><strong>用户登录名</strong></td>
                    <td style="padding:0px;"><strong>用户名称</strong></td>
                    <td style="padding:0px;"><strong>状态</strong></td>
                </tr>
        <s:iterator value="userList" id="ul" status="num">
            <tr class='tr<s:property value="#num.index%2"/>' onMouseMove="javascript:this.bgColor='#ddf5d4';" onMouseOut="javascript:this.bgColor='#FFFFFF';">
                <td align="center">
                        <input type="checkbox" name="choiceBox" value="<s:property value="#ul.userid"/>" id="<s:property value="#ul.userid"/>"/>
                </td>
                <td>
                    <a href="#" onclick="toEdit('<s:property value="#ul.userid"/>','<s:property value="#ul.username"/>','<s:property value="#ul.fzdwcode"/>')" style="text-decoration: none;">
                        <s:property value="#ul.userid"/></a>
                </td>
                <td style="padding-left:5px;">
                    <s:property value="#ul.username"/>
                </td>
                <td style="padding-left:5px;">
                    <s:if test="#ul.status==\"1\"">正常</s:if><s:else>停用</s:else>
                </td>
            </tr>
        </s:iterator>
        <tr>
            <td colspan="4" align="left" style="padding-left:6px;">
                <button type="button" style="width:50px;" onclick="checkAll()">全选</button>
                &nbsp;
                <button type="button" style="width:50px;" onclick="nocheck()">反选</button>
                &nbsp;
                <button type="button" onClick="resetPassword()">重置密码</button>
                &nbsp;
                <button type="button" style="width:50px;" onClick="delUser()">停用</button>
                &nbsp;
                <button type="button" style="width:50px;" onClick="toAddUser()">新增</button>
            </td>
        </tr>
            </table>
        </td>
    </tr>
</table>

        <div id="editdiv" style="display:none;">
            <div id="titlediv" style="font-size:14pt;font-weight:bold;color:#15428B;padding-top:15px;padding-bottom:15px;text-align:center;">
            </div>
            <table width="100%"  border="0" cellpadding="0" cellspacing="0" class="form1">
              <tr>
                <th style="width:40%">用户登录名</th>
                <td><s:textfield name="userId" id="userId" theme="simple" onblur="checkInput(this)" cssClass="listinput" cssStyle="width:240px;"/>
                <span id="lblMsg" style="color:#FF0000;"></span></td>
              </tr>
              <tr>
                <th>用户名称</th>
                <td><s:textfield name="userName" id="userName" theme="simple" onblur="checkInput1(this)" cssClass="listinput" cssStyle="width:240px;"/>
                <span id="lblMsg1" style="color:#FF0000;"></span></td>
              </tr>
              <tr>
                <td>&nbsp;</td>
                <td><button type="button" id="saveBt" style="width:50px;" onclick="doAddUserInfo()">保存</button></td>
              </tr>
            </table>
        </div>
        <s:hidden name="dateSet"/>
</form>
</body>
<script language="javascript">
    parent._setHeight();

    function resetPassword(){
        var haveCheck=false;
        if (document.all.choiceBox) {
             if (document.all.choiceBox.length) {
                for (var a = 0; a < document.all.choiceBox.length; a ++) {
                     if (document.all.choiceBox[a].checked) {
                           haveCheck=true;
                     }
                }
             } else {
                 if (document.all.choiceBox.checked) {
                      haveCheck=true;
                 }
             }
        } else {
            alert("没有可以重置密码的用户！");
            return;
        }
        if(!haveCheck){
            alert("请选择需要重置密码的用户！");
            return;
        }
        if(!confirm("您确定重置这些用户的密码吗？")){
            return;
        }
        document.forms[0].action = "<c:url value='/system/resetPassword.do'/>";
        document.forms[0].submit();
    }

    function delUser(){
        var haveCheck=false;
        if (document.all.choiceBox) {
             if (document.all.choiceBox.length) {
                for (var a = 0; a < document.all.choiceBox.length; a ++) {
                     if (document.all.choiceBox[a].checked) {
                           haveCheck=true;
                     }
                }
             } else {
                 if (document.all.choiceBox.checked) {
                      haveCheck=true;
                 }
             }
        } else {
            alert("没有可以停用的用户！");
            return;
        }
        if(!haveCheck){
            alert("请选择需要停用的用户！");
            return;
        }
        if(!confirm("您确定停用这些用户吗？")){
            return;
        }
        document.forms[0].action = "<c:url value='/system/delUserInfo.do'/>";
        document.forms[0].submit();
    }

    function doAddUserInfo(){
        checkInput(document.getElementById("userId"));
        checkInput1(document.getElementById("userName"));
        if (!document.getElementById("userId").readOnly) {
            if(document.getElementById("lblMsg").innerText != "√") {
                if (document.getElementById("lblMsg").innerText=="该用户登录名已被停用！") {
                    if(!confirm("该用户登录名已被停用,选择“确定”将重新启用该用户并将密码重置为默认密码。")){
                        return;
                    }
                } else {
                    alert(document.getElementById("lblMsg").innerText);
                    document.getElementById("userId").focus();
                    return;
                }
            }
        }
        if(document.getElementById("lblMsg1").innerText != "√") {
            alert(document.getElementById("lblMsg1").innerText);
            document.getElementById("userName").focus();
            return;
        }
        if(!confirm("您确定保存该用户信息吗？")) {
            return;
        }
        document.getElementById("saveBt").disabled = true;
        document.forms[0].action = "<c:url value='/system/doAddUserInfo.do'/>";
        document.forms[0].submit();
    }

    function checkInput(obj) {
        if (!document.getElementById("userId").readOnly) {
            var lblMsg = document.getElementById("lblMsg");

            //创建正则表达式
            var re = /^[0-9a-zA-Z]{3,20}$/; //只输入数字和字母的正则
            //var re=/^[\u4e00-\u9fa5]{1,10}$/; //只输入汉字的正则

            obj.value = allTrim(obj.value);
            if (obj.value.search(re) == -1) {
                lblMsg.innerText = "用户登录名请输入数字或字母，长度3-30!";
                //lblMsg.innerText = "请输入汉字，字符不能超过十个。";
                return false;
            } else {
                var thetab = document.getElementById("queryResult");
                if(thetab.rows.length>2) {
                    var allrows = thetab.rows;
                    for(var a = 1;a < allrows.length-1;a ++) {
                        var allcol = allrows[a].cells;
                        var theid = allTrim(allcol[1].innerText);
                        if(theid == obj.value) {
                            lblMsg.innerText = "该用户登录名已被使用！";
                            return false;
                        }
                    }
                }
                lblMsg.innerText = "√";
                return true;
            }
        }
    }

    /*删除所有空格*/
    function allTrim(ui) {
        var notValid = /\s/;
        while (notValid.test(ui)) {
            ui = ui.replace(notValid, "");
        }
        return   ui;
    }

    function checkInput1(obj) {
        var lblMsg = document.getElementById("lblMsg1");
        obj.value = allTrim(obj.value);
        if (obj.value.length == 0 || obj.value.length > 40) {
            lblMsg.innerText = "请输入用户名称，长度不能超过40!";
            return false;
        } else {
            lblMsg.innerText = "√";
            return true;
        }
    }

    function toEdit(uid,uname,oid){
        document.getElementById("lblMsg").innerText = "";
        document.getElementById("lblMsg1").innerText = "";
        document.getElementById("editdiv").style.display = "block";
        document.getElementById("titlediv").innerText = "修改用户信息";
        document.getElementById("userId").value = uid;
        document.getElementById("userId").readOnly = true;
        document.getElementById("userName").value = uname;
        parent._setHeight();
    }

    function toAddUser(){
        document.getElementById("lblMsg").innerText = "";
        document.getElementById("lblMsg1").innerText = "";
        document.getElementById("editdiv").style.display = "block";
        document.getElementById("titlediv").innerText = "新增用户信息";
        document.getElementById("userId").value = "";
        document.getElementById("userId").readOnly = false;
        document.getElementById("userName").value = "";
        parent._setHeight();
    }

    function checkAll() {
        if (document.all.choiceBox) {
            if (document.all.choiceBox.length) {
                for (var a = 0; a < document.all.choiceBox.length; a ++) {
                    document.all.choiceBox[a].checked = true;
                }
            } else {
                document.all.choiceBox.checked = true;
            }
        }
    }

    function nocheck() {
        if (document.all.choiceBox) {
            if (document.all.choiceBox.length) {
                for (var a = 0; a < document.all.choiceBox.length; a ++) {
                    if (document.all.choiceBox[a].checked) {
                        document.all.choiceBox[a].checked = false;
                    } else {
                        document.all.choiceBox[a].checked = true;
                    }

                }
            } else {
                if (document.all.choiceBox.checked) {
                    document.all.choiceBox.checked = false;
                } else {
                    document.all.choiceBox.checked = true;
                }
            }
        }
    }
</script>
</html>